package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;


public class SetResponse extends AxdrType {
    private byte[] dataCode = null;
    private SetResponseChoice choice = SetResponseChoice._ERR_NONE_SELECTED;
    private SetResponseNormal setResponseNormal = null;
    private SetResponseNormalList setResponseNormalList = null;
    private SetThenGetResponseNormalList setThenGetResponseNormalList = null;

    public SetResponse() {
    }

    public void set_dataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setSetResponseNormal(SetResponseNormal setResponseNormal) {
        resetChoices();
        this.choice = SetResponseChoice.SET_RESPONSE_NORMAL;
        this.setResponseNormal = setResponseNormal;
    }

    public void setSetResponseNormalList(SetResponseNormalList setResponseNormalList) {
        resetChoices();
        this.choice = SetResponseChoice.SET_RESPONSE_NORMAL_LIST;
        this.setResponseNormalList = setResponseNormalList;
    }

    public void setSetThenGetResponseNormalList(SetThenGetResponseNormalList setThenGetResponseNormalList) {
        resetChoices();
        this.choice = SetResponseChoice.SET_THEN_GET_RESPONSE_NORMAL_LIST;
        this.setThenGetResponseNormalList = setThenGetResponseNormalList;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();
        codeLength += choosen.decode(input);
        resetChoices();
        this.choice = SetResponseChoice.getEnumByValue((int)choosen.getValue());
        if (this.choice == SetResponseChoice.SET_RESPONSE_NORMAL) {
            this.setResponseNormal = new SetResponseNormal();
            codeLength += this.setResponseNormal.decode(input);
            return codeLength;
        } else if (this.choice == SetResponseChoice.SET_RESPONSE_NORMAL_LIST) {
            this.setResponseNormalList = new SetResponseNormalList();
            codeLength += this.setResponseNormalList.decode(input);
            return codeLength;
        } else if (this.choice == SetResponseChoice.SET_THEN_GET_RESPONSE_NORMAL_LIST) {
            this.setThenGetResponseNormalList = new SetThenGetResponseNormalList();
            codeLength += this.setThenGetResponseNormalList.decode(input);
            return codeLength;
        }
        throw new Exception("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        if (this.dataCode != null) {
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
            return this.dataCode.length;
        }
        if (this.choice == SetResponseChoice._ERR_NONE_SELECTED) {
            throw new Exception("Error encoding AxdrChoice: No item in choice was selected.");
        }
        int codeLength = 0;
        if (this.choice == SetResponseChoice.SET_THEN_GET_RESPONSE_NORMAL_LIST) {
            if (this.setThenGetResponseNormalList != null) {
                codeLength += this.setThenGetResponseNormalList.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(3);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("setThenGetResponseNormalList is null");
            }
        } else if (this.choice == SetResponseChoice.SET_RESPONSE_NORMAL_LIST) {
            if (this.setResponseNormalList != null) {
                codeLength += this.setResponseNormalList.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(2);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("setResponseNormalList is null");
            }
        } else if (this.choice == SetResponseChoice.SET_RESPONSE_NORMAL) {
            if (this.setResponseNormal != null) {
                codeLength += this.setResponseNormal.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("setResponseNormal is null");
            }
        }
        throw new Exception("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    private void resetChoices() {
        this.choice = SetResponseChoice._ERR_NONE_SELECTED;
        this.setResponseNormal = null;
        this.setResponseNormalList = null;
        this.setThenGetResponseNormalList = null;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    public SetResponseChoice getChoiceIndex() {
        return this.choice;
    }

    public String toString() {
        if (this.choice == SetResponseChoice.SET_RESPONSE_NORMAL) {
            if (this.setResponseNormal != null) {
                return "choice: {setResponseNormal: " + this.setResponseNormal + "}";
            } else {
                return "choice is setResponseNormal but setResponseNormal is null";
            }
        } else if (this.choice == SetResponseChoice.SET_RESPONSE_NORMAL_LIST) {
            if (this.setResponseNormalList != null) {
                return "choice: {setResponseNormalList: " + this.setResponseNormalList + "}";
            } else {
                return "choice is setResponseNormalList but setResponseNormalList is null";
            }
        } else if (this.choice == SetResponseChoice.SET_THEN_GET_RESPONSE_NORMAL_LIST) {
            if (this.setThenGetResponseNormalList != null) {
                return "choice: {setThenGetResponseNormalList: " + this.setThenGetResponseNormalList + "}";
            } else {
                return "choice is setThenGetResponseNormalList but setThenGetResponseNormalList is null";
            }
        } else {
            return "unknown";
        }
    }

    public enum SetResponseChoice {
        _ERR_NONE_SELECTED(-1),
        SET_RESPONSE_NORMAL(1),
        SET_RESPONSE_NORMAL_LIST(2),
        SET_THEN_GET_RESPONSE_NORMAL_LIST(3);

        private final int value;

        SetResponseChoice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static SetResponseChoice getEnumByValue(int value) {
            for (SetResponseChoice item : SetResponseChoice.values()) {
                if (value == item.getValue()) {
                    return item;
                }
            }
            return null;
        }
    }
}



